//
//  CZTranslateView.m
//  globalTranslate
//
//  Created by 陈镇 on 2025/8/9.
//

#import "CZTranslateView.h"
#import "CZAVAudioManager.h"
#import "SLUserInfoModel.h"
#import "CZSelectFontSizeView.h"


@interface CZTranslateView ()<CZAVAudioManagerDelegate>


@property(nonatomic,strong) QMUILabel *text;
@property(nonatomic,strong) QMUIButton *playBtn;
@end
@implementation CZTranslateView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.text];
        [self addSubview:self.playBtn];
    }
    return self;
}

-(QMUILabel *)text{
    if (!_text) {
        
        _text = [QMUILabel createTextColor:UIColorHex(#222222) font:SLFontMakeRegular(15) textAlignment:NSTextAlignmentLeft];
        _text.numberOfLines = 0;
    }
    return _text;
}
-(QMUIButton *)playBtn{
    if (!_playBtn) {
        _playBtn = [QMUIButton createButtonImageName:@"shengyin" selectImageName:@"shengyin"];
        [_playBtn addTarget:self action:@selector(clickPlay) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _playBtn.frame = CGRectMake(self.width - 16, 0, 16, 20);
    _text.frame = CGRectMake(0, 0, _playBtn.left - 10, self.height);
}
-(CGSize)sizeThatFits:(CGSize)size{
    
    CGSize size_1= [_text sizeThatFits:CGSizeMake(size.width - 16 - 10, CGFLOAT_MAX)];

    return CGSizeMake(size_1.width + 16 + 10, size_1.height);
    
}

-(void)setTextString:(NSString *)textString{
    _textString = [textString qmui_trim];
    _text.text = _textString;
    
    NSInteger index = [[SLUserInfoModel shareManage].sizeIndex intValue];
    UIFont *font =  [CZSelectFontSizeView fontList][index];
    _text.font = font;
    
    
    
    self.size = [self sizeThatFits:self.size];
    [self layoutSubviews];
}

-(void)setTextURL:(NSString *)textURL{
    _textURL = textURL;
    _playBtn.hidden = (textURL.length == 0);

}
-(void)clickPlay{

//    [_playBtn playAnimat];

    [[CZAVAudioManager shareManager] playVoiceURL:self.textURL delegate:self];

}

-(void)auidoPlayDidFinishSuccess:(BOOL)success{
    
    
    
}

@end
